package gzhaoku.com.common.util;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import org.apache.commons.lang3.StringUtils;

import gzhaoku.com.common.Logger;
import gzhaoku.com.common.ui.annotations.Router;

public class Routers {

    static Logger log = Logger.getLog(Routers.class);

    public static Class getClassByRoute(Activity activity, String path){
        ActivityInfo[] activityInfos = getActivityList(activity);
        for(ActivityInfo a:activityInfos){
            try {
                Class c = Class.forName(a.name);
                if(c.isAnnotationPresent(Router.class)){
                    Router router = (Router) c.getAnnotation(Router.class);
                    if(StringUtils.equalsAnyIgnoreCase(path, router.value())){
                        return c;
                    }
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    private static ActivityInfo[] getActivityList(Activity activity) {
        try{
            PackageManager pm = activity.getPackageManager();

            PackageInfo info = pm.getPackageInfo(activity.getApplicationContext().getPackageName(), PackageManager.GET_ACTIVITIES);

            ActivityInfo[] list = info.activities;

            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

}
